# from emailsend import sendMail
import time

# sendMail(fromAddress = "ruangnazi@163.com", toAddress = "326737833@qq.com", mSubject = "ip address", mBody = "message body", mBodyHTML = arrow.now().format() +'  ' + my_ip , serverLogin = True, serverServer = "smtp.163.com", serverTSL = True, serverUser = "ruangnazi@163.com", serverPass = "Rider001")


from wxpy import *
bot = Bot(cache_path='./wxcache',console_qr=True)


def init_wx():

    # # 向文件传输助手发送消息
    # bot.file_helper.send('Hello from wxpy!')

    blokc_list = ["内测", '投资资讯']
    boring_group = [bot.groups().search(each)[0] for each in blokc_list]
    tuling = Tuling(api_key='2fd7c4696e534208840f482b4239c8d8')

    def add_friend():
        friends = bot.friends()
        group = bot.groups().search('内测')[0]

        fds = []
        for friend in friends:
            if friend not in group:
                fds.append(friend)
        if fds:
            group.add_members(fds)

    # 使用图灵机器人自动与指定好友聊天
    @bot.register()
    @dont_raise_response_error
    def reply_my_friend(msg):
        add_friend()
        tuling.do_reply(msg)

    @bot.register(msg_types=FRIENDS)
    @dont_raise_response_error
    def auto_accept_friends(msg):
        # # 判断好友请求中的验证文本
        new_friend = bot.accept_friend(msg.card)
        # 或 new_friend = msg.card.accept()
        # 向新的好友发送消息
        new_friend.send('哈哈，很高心遇见你')
        time.sleep(2)
        group.add_members(new_friend)

    @bot.register(boring_group)
    def ignore(msg):
        return

    # 找到需要接收日志的群 -- `ensure_one()` 用于确保找到的结果是唯一的，避免发错地方
    group_receiver = ensure_one(bot.groups().search('投资资讯'))
    # 指定这个群为接收者
    # logger = get_wechat_logger(group_receiver)
    # return logger
    return group_receiver
    
logger = init_wx()